
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Bootloader_ReadDataById_0x22_test.py
@Time    :   2022/09/01 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from numpy import byte, bytes_, int16
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
import Set_up
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

DID = [('22 F1 80', Set_up.VehicleManufacturerECUBootSoftwareVersionNumber, '2'),    # VehicleManufacturerECUBootSoftwareVersionNumber 底层软件Bootload版本号
       # ECUHardwareVersionNumber 硬件版本号
       ('22 F1 93', '62 F1 93' + Set_up.ECUHardwareVersionNumber, '2'),
       # ECUSoftwareCode 软件编码
       ('22 F1 94', '62 F1 94' +  Set_up.ECUSoftwareCode, '2'),
       # ECUSoftwareVersionNumber 软件版本号
       ('22 F1 95', [0x62,0xF1,0x95] + Set_up.ECUSoftwareVersionNumber, '2')
      ]


DID_NRC13 = [('22', '7F 22 13'),
             ('22 F1', '7F 22 13'),
             ('22 10', '7F 22 13'),
             ('22 F1 80 01', '7F 22 13'),
             ('22 10 11 01', '7F 22 13'),
             ('22 01 03 01', '7F 22 13'),
             ('22 02', '7F 22 13'),
             ('22 02 8D 01', '7F 22 13'),
             ('22 F1 90 01', '7F 22 13'),
             ('22 F1 94 01', '7F 22 13'),
             ('22 F1 95 FF', '7F 22 13'),
             ('22 34', '7F 22 13')
             ]

DID_NRC31_NoSupportProgramming = [('22 FF 94', '7F 22 31'),
                                  ('22 FF 95', '7F 22 31'),
                                  ('22 34 01', '7F 22 31'),
                                  ('22 21 00', '7F 22 31'),
                                  ('22 F1 90', '7F 22 31'),
                                  ('22 02 85', '7F 22 31'),
                                  ('22 02 8D', '7F 22 31'),
                                  ('22 10 11', '7F 22 31'),
                                  ('22 34 04', '7F 22 31'),
                                  ('22 34 03', '7F 22 31'),
                                  ('22 34 41', '7F 22 31'),
                                  ('22 34 42', '7F 22 31'),
                                  ('22 01 03', '7F 22 31')
                                  ]


class Test_ReadDataById_Phy(object):

    def ReadDID(self,Driver, req, resp, session):
        iStep = 1
        if req == '22 10 11':
        # 设置车速>3kph
            Driver['CAN'].write_db_signal('Vehicle_CAN', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 40)
            time.sleep(0.5)
            msg = Driver['CAN'].read_db_signal('Vehicle_CAN', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S')
            logging.info('speed is:{0}'.format(msg))
            time.sleep(0.5)
        if req == '22 F1 90':
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E F1 90 00 00 31 32 33 34 35 36 37 38 39 00 00 00 00 00 00", '6E F1 90', 3, iStep, True)
        if req == '22 21 00':
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2e f1 a1 ff ff ff ff ff ff ff ff", '6e f1 a1', 3, iStep, True)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2e 21 00 ' + Set_up.VariantCoding[0], '6e 21 00', 3, iStep, True)
        
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, session, iStep)
        if req == '22 10 11':
            # 设置车速<3kph
            Driver['CAN'].write_db_signal('Vehicle_CAN', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 0)




    ids = [f'{req}' for req, resp, session in DID]

    @allure.severity('normal')
    @allure.title('Test {req} of read data by identifier 0x22')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp,session', DID, ids=ids)
    def test_ReadDataById_Programming_Phy(self, action, Driver, req, resp, session):
        time.sleep(2)
        self.ReadDID(Driver, req, resp, 2)

    ids = [f'{req}' for req, resp in DID_NRC31_NoSupportProgramming]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC31 in programming session with function addressing')
    @allure.description('Test 0x22 NRC31 in programming session with function addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC31_NoSupportProgramming, ids=ids)
    def test_ReadDataById_NRC13_Programming_phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

    ids = [f'{req}' for req, resp in DID_NRC13]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC13 in programming session with function addressing')
    @allure.description('Test 0x22 NRC13 in programming session with function addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC13, ids=ids)
    def test_ReadDataById_NRC13_Programming_phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(2)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)